package com.ckd.flyingtrip.utils;


import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DataCheck {

	// 检查签名
	public static String signCheck(String str)  {
		String json = str;
		@SuppressWarnings("unchecked")
		Map<String, String> paramMap = (Map<String, String>) JSONObject.parse(str);
		paramMap.put("appId", "fgcx123");
		paramMap.put("version", "fgcx321");
		Log.i("字符串a",paramMap.toString());
		// 加密前字符串拼接
		Set<String> keySet = paramMap.keySet();
		Log.i("字符串b",keySet.toString());

		List<String> keyList = new ArrayList<>(keySet);
		Log.i("字符串c",keyList.toString());

		Collections.sort(keyList);
		Log.i("字符串d",keyList.toString());

		StringBuilder signStr = new StringBuilder();

		for (String key : keyList) {

			if (key.equals("sign")) {
				continue;
			}

			Object value = paramMap.get(key);

			if (value == null || (value.getClass().isArray()
					&& byte.class.isAssignableFrom(value.getClass().getComponentType()))) {
				continue;
			}

			String valueString = value.toString();

			if (StringUtils.isEmpty(valueString)) {
				continue;
			}
			signStr.append(key).append(value);

		}
		Log.i("字符串f",signStr.toString());
		org.json.JSONObject jsons = null;
		try {
		 	jsons = new org.json.JSONObject(json);
			jsons.put("sign",SHA1Util.Sha1(signStr.toString().toLowerCase()));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Log.i("请求json",jsons.toString());
		return  jsons.toString();

	}







	// 检查身份证号
	public static boolean isIdCard(String idCard) {

		String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
				+

				"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

		return idCard.matches(regularExpression) ? true : false;
	}
}
